package com.skytech.tax.customsdatawarehouse.electronicport.proceedspayment.importpayment.domain.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.skytech.tax.customsdatawarehouse.common.util.SecurityUtil;
import com.skytech.tax.customsdatawarehouse.electronicport.proceedspayment.exportproceeds.domain.entity.ExportProceeds;
import com.skytech.tax.customsdatawarehouse.electronicport.proceedspayment.exportproceeds.domain.service.ExportProceedsService;
import com.skytech.tax.customsdatawarehouse.electronicport.proceedspayment.importpayment.domain.dao.ImportPaymentDao;
import com.skytech.tax.customsdatawarehouse.electronicport.proceedspayment.importpayment.domain.dao.ImportPaymentFileDao;
import com.skytech.tax.customsdatawarehouse.electronicport.proceedspayment.importpayment.domain.dao.ImportPaymentSimpleDao;
import com.skytech.tax.customsdatawarehouse.electronicport.proceedspayment.importpayment.domain.entity.ImportPayment;
import com.skytech.tax.customsdatawarehouse.electronicport.proceedspayment.importpayment.domain.entity.ImportPaymentFile;
import com.skytech.tax.customsdatawarehouse.electronicport.proceedspayment.importpayment.domain.entity.ImportPaymentSimple;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Lianhong
 * @since 2021/11/19
 */
@Service
public class ImportPaymentService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExportProceedsService.class);

    @Value("${customsdatawarehouse.token}")
    private String token;

    @Resource
    private ImportPaymentDao importPaymentDao;
    @Resource
    private ImportPaymentSimpleDao importPaymentSimpleDao;
    @Resource
    private ImportPaymentFileDao importPaymentFileDao;

    public String save(String importPayment) {
        if (StringUtils.isEmpty(importPayment)) return StringUtils.EMPTY;
        String text = SecurityUtil.decrypt3DES(token, importPayment);
        try {
            JSONObject req = JSON.parseObject(text);
            if (req == null) return StringUtils.EMPTY;

            String entryId = req.getString("entryId");
            String socialCreditCode = req.getString("socialCreditCode");
            JSONObject paymentObj = req.getJSONObject("importPayment");
            ImportPayment payment = new ImportPayment(entryId, socialCreditCode, paymentObj);

            ImportPaymentSimple simple = payment.buildImportPaymentSimple();
            if (simple == null) return StringUtils.EMPTY;

            this.remove(payment.getEntryId());

            importPaymentDao.save(payment);
            importPaymentSimpleDao.save(simple);
            return SecurityUtil.encrypt3DES(token, payment.getImportPayment());
        } catch (Exception e) {
            LOGGER.error("保存进口付汇错误", e);
            return StringUtils.EMPTY;
        }
    }

    public String details(String entryId) {
        ImportPayment payment = importPaymentDao.findByEntryId(entryId);
        if (payment == null) return StringUtils.EMPTY;
        return SecurityUtil.encrypt3DES(token, payment.getImportPayment());
    }

    public void remove(String entryId) {
        importPaymentDao.delete(entryId);
        importPaymentSimpleDao.delete(entryId);
    }

    public boolean exists(String entryId) {
        return importPaymentSimpleDao.exists(entryId);
    }

    public String importPaymentFile(String entryId) {
        return importPaymentFileDao.findByEntryId(entryId);
    }

    public void saveImportPaymentFile(String entryId, String storedFile) {
        ImportPaymentFile importPaymentFile = new ImportPaymentFile(entryId, storedFile);
        importPaymentFileDao.save(importPaymentFile);
    }

    public boolean importPaymentFileStored(String entryId) {
        return importPaymentFileDao.exists(entryId);
    }

    public String existsImportPayments(List<String> entryIds) {
        if (CollectionUtils.isEmpty(entryIds)) return StringUtils.EMPTY;
        List<List<String>> parts = Lists.partition(entryIds, 500);
        List<String> result = new ArrayList<>();
        parts.forEach(item -> result.addAll(importPaymentSimpleDao.exists(item)));
        return StringUtils.join(result, ",");
    }
}
